<template>
	<view class="wrap" :class="status == 4 ? 'normal' : ''">
		<u-loading-page :loading="loading"></u-loading-page>
		<view v-if="!loading">
			<view class="header">
				<view class="flex">
					<u-icon name="arrow-left" color="#333333" size="24" @click="back"></u-icon>
				</view>
				</u-navbar>
				<view class="title" v-text="auditText(status)"></view>
				<!-- // 1.待审核 2.通过 3.驳回 4.申诉中 -->
				<image v-if="status > 2" src="@/static/bg_warning.png" mode=""></image>
				<image v-if="status == 1" src="@/static/bg_success.png" mode=""></image>
			</view>
			<view class="body">
				<view class="name">
					<!-- <view class="flex">
						<text class="tips">活体</text>
						<text class="plate">{{driverInfo.driverName}}</text>
					</view> -->
					<view class="type">
						<text>{{driverInfo.mobile}}</text>
						<text class="ring" @click="makePhoneCall(driverInfo.mobile)">联系司机</text>
					</view>
				</view>
				<view class="item">
					<view class="flex">
						<view class="gray">姓名：</view>
						<view class="black">{{driverInfo.driverName}}</view>
					</view>
					<view class="flex">
						<view class="gray">手机号：</view>
						<view class="black">{{driverInfo.mobile}}</view>
					</view>
					<view class="flex">
						<view class="gray">身份证号码：</view>
						<view class="black">{{driverInfo.idcard}}</view>
					</view>
					<view class="flex">
						<view class="gray">身份证有效期：</view>
						<view class="black">{{formatTime(driverInfo.idcardStartDate)}}-{{formatTime(driverInfo.idcardEndDate)}}</view>
					</view>
					<view class="flex">
						<view class="gray">身份证照片：</view>
						<view class="black" @click="photoInfo(0)" v-if="hasIdcard">
							<view>已上传</view>
							<view class="check">点击查看</view>
						</view>
						<view class="black" v-else>
							<view>未上传</view>
						</view>
					</view>
				</view>
				<view class="item">
					<view class="flex">
						<view class="gray">准驾车型：</view>
						<view class="black">{{driverInfo.driverModel}}</view>
					</view>
					<view class="flex">
						<view class="gray">发证机关：</view>
						<view class="black">{{driverInfo.issuingOrganizations}}</view>
					</view>
					<view class="flex">
						<view class="gray">驾驶证有效期：</view>
						<view class="black">{{formatTime(driverInfo.driverStartDate)}}~{{formatTime(driverInfo.driverEndDate)}}</view>
					</view>
					<view class="flex">
						<view class="gray">驾驶证照片：</view>
						<view class="black" @click="photoInfo(1)" v-if="hasDriving">
							<view>已上传</view>
							<view class="check">点击查看</view>
						</view>
						<view class="black" v-else>
							<view>未上传</view>
						</view>
					</view>
				</view>
				<view class="item">
					<view class="flex">
						<view class="gray">从业资格证照片：</view>
						<view class="black" @click="photoInfo(2)" v-if="hasTqc">
							<view>已上传</view>
							<view class="check">点击查看</view>
						</view>
						<view class="black" v-else>
							<view>未上传</view>
						</view>
					</view>
				</view>
				<view class="item">
					<view class="flex">
						<view class="gray">申请时间：</view>
						<view class="black">{{date.crtDate || driverInfo.crtTime}}</view>
					</view>
				</view>

				<view class="shade" v-if="status == 3">
					<view class="explain">
						<view class="left">失败说明:</view>
						<view class="right">{{date.reject}}</view>
					</view>
				</view>
			</view>
			<view class="footer">
				<view class="btnBox" v-if="status == 1">
					<view class="reject btn" @click="showReject = true">驳回</view>
					<view class="pass btn" @click="onsShowProject">通过</view>
				</view>
				<view class="submit" @click="goRegister" v-if="status == 3">重新提交</view>
			</view>
		</view>
		
		<!-- 弹出层 -->
		<u-popup :show="showProject" @close="showProject = false" :round="10">
			<view class="popup-wrap">
				<view class="title">选择项目</view>
				<u-search v-model="keyProject" shape="square" actionText="搜索" placeholder="搜索项目" :animation="true" @custom="searchProject" @search="searchProject">
				</u-search>
				<view class="popup-empty" v-if="!user_project.length">暂无数据～～～</view>
				<view class="popup-list" v-else>
					<u-checkbox-group v-model="valueProject" @change="checkboxChange" :borderBottom="true"
						placement="column" iconPlacement="right" size="16" activeColor="#00468C">
						<u-checkbox shape="circle" labelSize="15" labelColor="#333"
							v-for="(item, index) in user_project" :key="index" :label="item.project_name"
							:name="item.project_id">
						</u-checkbox>
					</u-checkbox-group>
				</view>
				<view class="checkboxBox">
					 <u-checkbox-group placement="column" iconPlacement="right" size="16" activeColor="#00468C"  v-model="isAddDriver">
						<u-checkbox :borderBottom="true"
						 label="授权司机自动添加新建项目" name='1'></u-checkbox>
					</u-checkbox-group>
				</view>
				<view class="popup-btn">
					
					<view class="cancel btn" @click="showProject = false">取消</view>
					<view class="confirm btn" @click="showProject = false; showPass = true">确认</view>
				</view>
			</view>
		</u-popup>
		
		<u-modal content="确定该司机通过审核？" :show="showPass" showCancelButton closeOnClickOverlay @confirm="confirmPass"
			@cancel="showPass = false" @close="showPass = false"></u-modal>
		<u-modal title="驳回原因" :show="showReject" closeOnClickOverlay @confirm="confirmReject"
			@close="showReject = false, valueReject = ''">
			<textarea v-model="valueReject" placeholder-style="color:#A1A1A1" placeholder="请输入驳回理由,最多30个字符" maxlength="30"/>
		</u-modal>
	</view>
</template>

<script>
	import {
		driverDetail,
		driverReject,
		driverPass,
		getAllReject
	} from '@/config/api/app.js'
	import Cache from '@/utils/cache'
	import {
		formatDate
	} from '@/utils/tools'
	export default {
		data() {
			return {
				isAddDriver: [],
				loading: true,
				status: null,
				showPass: false,
				showReject: false,
				valueReject: '',
				driverEtpId: '',
				
				driver_id: '',
				driverInfo: null,
				user_project: [],
				date: null,
				projects: [],

				idFlag: false,
				driverFlag: false,
				hasIdcard: false,
				hasDriving: false,
				hasTqc: false,
				
				showProject: false,
				keyProject: '',
				valueProject: '',
			}
		},
		async onLoad(options) {
			let info = JSON.parse(options.info)
			this.driver_id = info.driver_id
			this.status = info.status
			this.driverEtpId = info.driver_etp_id
			await this.getDetail()
		},
		methods: {
			checkboxChange(e){
				this.projects = e
			},
			searchProject(e) {
				this.keyProject = uni.$u.trim(e)
				this.getProject(1)
			},
			//	获取项目列表
			getProject(type) {
				let data = {
					params: {
						projectName: this.keyProject
					},
					custom: {}
				}
				if (type === 0) {
					data.custom.title = '获取项目……'
				}
				getAllReject(data).then((res) => {
					if (res.code == 0) {
						this.user_project = res.data.project
						// for (let i = 0;i<100;i++) {
						// 	this.user_project.push(res.data.project)
						// }
					} else {
						uni.$u.toast(res.msg);
					}
				}).catch(err => {
					console.log(err)
				})
			},
			onsShowProject() {
				this.getProject(1)
				this.showProject = true
			},
			//	获取司机详情
			getDetail() {
				let params = {
					driver_id: this.driver_id
				}
				driverDetail({
					params: params
				}).then((res) => {
					if (res.code == 0) {
						this.driverInfo = res.data.driver
						this.user_project = res.data.user_project
						this.date = res.data.date
						this.isAddDriver = this.date.isAddDriver == 1?['1']:[]
						this.loading = false
						this.idFlag = this.computeDate(this.driverInfo.idcardEndDate)
						this.driverFlag = this.computeDate(this.driverInfo.driverEndDate)
						this.hasIdcard = this.hasPhoto(this.driverInfo.idcard1, this.driverInfo.idcard2)
						this.hasDriving = this.hasPhoto(this.driverInfo.driverLicense, this.driverInfo
							.drivingLicenseDeputy)
						this.hasTqc = this.hasPhoto(this.driverInfo.tqc, this.driverInfo.tqcDeputy)
						this.user_project.forEach(item => {
							this.projects.push(item.project_id)
						})
					} else {
						uni.$u.toast(res.msg);
					}
				}).catch(err => {
					console.log(err)
				})
			},
			//	通过
			confirmPass() {
				this.showProject = false
				let params = {
					driverEtpId: this.driverEtpId,
					projects: this.projects,
					isAddDriver: this.isAddDriver.length == 1?1:0
				}
				driverPass(params).then((res) => {
					console.log(res.data)
					if (res.code == 0) {
						this.showPass = false
						uni.$u.toast("审核通过");
						setTimeout(() => {
							uni.navigateBack()
						}, 600)
					} else {
						this.showPass = false
						uni.$u.toast(res.msg);
					}
				}).catch(err => {
					console.log(err)
				})
			},
			//	驳回
			confirmReject() {
				if (!uni.$u.trim(this.valueReject)) {
					uni.$u.toast("请先输入驳回理由");
					return
				}
				let params = {
					driverEtpId: this.driverEtpId,
					reason: uni.$u.trim(this.valueReject)
				}
				driverReject({
					params: params
				}).then((res) => {
					console.log(res.data)
					if (res.code == 0) {
						this.showReject = false
						uni.$u.toast("驳回通过");
						setTimeout(() => {
							uni.navigateBack()
						}, 600)
					} else {
						this.showReject = false
						this.valueReject = ''
						uni.$u.toast(res.msg);
					}
				}).catch(err => {
					console.log(err)
				})
			},
			//	查看相册
			photoInfo(num) {
				// 0是身份证， 1是驾驶证， 2是从业资格证
				const {
					idcard1,
					idcard2,
					driverLicense,
					drivingLicenseDeputy,
					tqc,
					tqcDeputy
				} = this.driverInfo
				let urls = []
				if (num === 0) {
					if (idcard1) urls.push(idcard1)
					if (idcard2) urls.push(idcard2)
				}
				if (num === 1) {
					if (driverLicense) urls.push(driverLicense)
					if (drivingLicenseDeputy) urls.push(drivingLicenseDeputy)
				}
				if (num === 2) {
					if (tqc) urls.push(tqc)
					if (tqcDeputy) urls.push(tqcDeputy)
				}
				uni.previewImage({
					indicator: "number",
					loop: true,
					urls: urls,
					success: () => {
						console.log('图片展示成功')
					}
				})
			},
			//	时间是否过期
			computeDate(date) {
				let endDate = new Date(date)
				let currentDate = new Date()
				if (endDate > currentDate) {
					return false
				} else {
					return true
				}
			},
			//	身份证，驾驶证，从业资质图片是否有上传
			hasPhoto(front, reverse) {
				if (!front && !reverse) {
					return false
				} else {
					return true
				}
			},
			//	拨打电话
			makePhoneCall(phone) {
				uni.makePhoneCall({
					phoneNumber: phone.toString(),
					success: function(e) {
						console.log(e);
					},
					fail: function(e) {
						console.log(e);
					}
				})
			},
			back() {
				uni.navigateBack()
			},
			goRegister() {
				uni.navigateTo({
					url: `/pages/driver/register?id=${this.driver_id}`
				})
			},
			auditText(status) {
				// 1.待审核 2.通过 3.驳回 4.申诉中
				switch (status) {
					case '1':
						return '待审核…'
						break
					case '2':
						return '已通过…'
						break
					case '3':
						return '已驳回…'
						break
					case '4':
						return '申诉中…'
						break
				}
			},
			formatTime(str) {
				return formatDate(str)
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #FAFAFA;
	}
	.u-checkbox{
		padding: 28rpx 0!important;
	}
	
	.popup-wrap {
		padding: 32rpx;
	
		.popup-empty {
			text-align: center;
			color: #c0c4cc;
			font-size: 30rpx;
			padding: 32rpx 0;
		}
	
		.title {
			text-align: center;
			font-size: 32rpx;
			font-weight: 500;
			color: #333333;
			margin-bottom: 24rpx;
		}
	
		.popup-btn {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 12rpx 0 calc(12rpx + env(safe-area-inset-bottom));
	
			.btn {
				font-size: 30rpx;
				font-weight: 500;
				line-height: 80rpx;
				border-radius: 12rpx;
				text-align: center;
				box-shadow: 0 0 20rpx 12rpx rgba(0, 62, 129, 0.04);
				box-sizing: border-box;
			}
	
			.cancel {
				color: #333333;
				width: 40%;
				background: #fff;
				border: 1rpx solid #e9e9e9;
			}
	
			.confirm {
				width: 55%;
				color: #FFFFFF;
				background: linear-gradient(to right, #4d83b9, #003E81);
			}
		}
	
		.popup-list {
			max-height: 460rpx;
			box-sizing: border-box;
			overflow: auto;
			margin: 12rpx 0;
	
			.item {
				border-bottom: 1rpx solid #F5F7FB;
	
				.u-radio {
					padding: 24rpx 0;
				}
	
				&:last-child {
					border-bottom: none;
				}
			}
		}
	}
	
	textarea {
		background: #F6F6F6;
		border-radius: 8rpx;
		padding: 24rpx;
	}

	.wrap {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		width: 100vw;
		min-height: 100vh;
		box-sizing: border-box;
		padding: 0 32rpx calc(128rpx + env(safe-area-inset-bottom));
		&.normal{
			padding: 0 32rpx calc(env(safe-area-inset-bottom));
		}
	}

	.footer {
		position: absolute;
		left: 32rpx;
		bottom: calc(24rpx + env(safe-area-inset-bottom));
		width: calc(100% - 64rpx);
		box-sizing: border-box;
		.btnBox {
			display: flex;
			align-items: center;
			justify-content: space-between;
		}

		.btn {
			font-size: 30rpx;
			font-weight: 500;
			line-height: 80rpx;
			border-radius: 12rpx;
			text-align: center;
			box-shadow: 0 0 20rpx 12rpx rgba(0, 62, 129, 0.04);
		}

		.submit {
			width: 100%;
			font-size: 30rpx;
			font-weight: 500;
			color: #FFFFFF;
			line-height: 80rpx;
			border-radius: 12rpx;
			text-align: center;
			background: linear-gradient(to right, #4d83b9, #003E81);
		}

		.reject {
			color: #333333;
			width: 40%;
			background: #fff;
		}

		.pass {
			width: 55%;
			color: #FFFFFF;
			background: linear-gradient(to right, #4d83b9, #003E81);
		}
	}

	.body {
		background: #FFFFFF;
		box-shadow: 0 0 20rpx 12rpx rgba(0, 62, 129, 0.04);
		border-radius: 8rpx;
		padding: 24rpx;
		margin-bottom: 32rpx;
		position: relative;

		.shade {
			position: absolute;
			top: 0;
			left: 0;
			right: 0;
			bottom: 0;
			background: rgba(255, 255, 255, 0.8);
			border-radius: 8rpx;
			padding: 24rpx;

			.explain {
				display: flex;
				font-size: 28rpx;
				line-height: 32rpx;
				font-weight: 500;
				background: #FFFFFF;
				box-shadow: 0 0 20rpx 0 rgba(141, 9, 12, 0.17);
				border-radius: 8rpx;
				margin-top: 130rpx;
				padding: 24rpx;
				box-sizing: border-box;

				.left {
					color: #9E9E9E;
					width: 130rpx;
					margin-right: 24rpx;
				}

				.right {
					color: #333333;
					width: calc(100% - 124rpx);
					word-break: break-all;
				}
			}

		}

		.item {
			border-top: 1rpx solid #F5F7FB;
			padding: 12rpx 0;

			.flex {
				display: flex;
				padding: 16rpx 0;
				font-size: 30rpx;
			}

			.gray {
				width: 40%;
				font-weight: 400;
				color: #9E9E9E;
			}

			.black {
				width: 60%;
				font-weight: 500;
				color: #333333;
				display: flex;

				&.list {
					flex-direction: column;

					text {
						margin-top: 12rpx;

						&:first-child {
							margin-top: 0;
						}
					}
				}

				.check {
					font-size: 22rpx;
					font-weight: 500;
					color: #003E81;
					padding: 5rpx 10rpx;
					background: #F0F7FF;
					margin-left: 12rpx;
				}
			}
		}

		.name {
			margin-bottom: 24rpx;

			.flex {
				display: flex;
				align-items: center;
				justify-content: flex-start;
			}

			.tips {
				font-size: 22rpx;
				font-weight: 500;
				color: #FFF;
				margin-right: 12rpx;
				padding: 6rpx 10rpx;
				background: #16C295;
			}

			.plate {
				font-size: 36rpx;
				font-weight: bold;
				color: #333333;
			}

			.type {
				font-size: 30rpx;
				font-weight: 500;
				color: #333333;
				line-height: 44rpx;
				margin-top: 20rpx;

				.ring {
					font-size: 22rpx;
					font-weight: 500;
					color: #003E81;
					background: #F0F7FF;
					border-radius: 4rpx;
					margin-left: 12rpx;
					padding: 3rpx 12rpx;
				}
			}
		}
	}

	.header {
		box-sizing: border-box;
		padding: 100rpx 0 32rpx;
		position: relative;

		.flex {
			display: flex;
			align-items: center;
		}

		.title {
			font-size: 68rpx;
			font-weight: 500;
			color: #333333;
			margin-top: 24rpx;
		}

		image {
			position: absolute;
			right: 0;
			bottom: 0;
			width: 386rpx;
			height: 226rpx;
			zoom: .8;
		}
	}
	.checkboxBox{
		border-top: 2rpx solid #c7c7c7;
	}
</style>
